package socrates.dto.dao;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import java.sql.SQLException;
import java.util.List;
import socrates.dto.*;
import socrates.ws.WSCursos;
import socrates.ws.WSLogin;

/**
 *
 * @author gcca
 */
public class Onix {

	private Onix() {
		try {
			iConx = new JdbcConnectionSource(
							"jdbc:h2:/data/data/socratesmovil/databases/soso");
			alumno = DaoManager.createDao(iConx, AlumnoDTO.class);
			clase = DaoManager.createDao(iConx, ClaseDTO.class);
			curso = DaoManager.createDao(iConx, CursoDTO.class);
			horario = DaoManager.createDao(iConx, HorarioDTO.class);
			inasistencia = DaoManager.createDao(iConx, InasistenciaDTO.class);
			nota = DaoManager.createDao(iConx, NotaDTO.class);
		} catch (SQLException e) {
			throw new RuntimeException("excepciOn iniciando base de datos", e);
		}
		try {
			TableUtils.createTableIfNotExists(iConx, AlumnoDTO.class);
			TableUtils.createTableIfNotExists(iConx, ClaseDTO.class);
			TableUtils.createTableIfNotExists(iConx, CursoDTO.class);
			TableUtils.createTableIfNotExists(iConx, HorarioDTO.class);
			TableUtils.createTableIfNotExists(iConx, InasistenciaDTO.class);
			TableUtils.createTableIfNotExists(iConx, NotaDTO.class);
		} catch (SQLException e) {
		}
	}

	public static Onix instancia() {
		if (onix == null) {
			onix = new Onix();
		}
		return onix;
	}
	private static Onix onix;
	private static ConnectionSource iConx;
	public static Dao<AlumnoDTO, Integer> alumno;
	public static Dao<ClaseDTO, Integer> clase;
	public static Dao<CursoDTO, Integer> curso;
	public static Dao<HorarioDTO, Integer> horario;
	public static Dao<InasistenciaDTO, Integer> inasistencia;
	public static Dao<NotaDTO, Integer> nota;

	public static void sinc() {
		try {
			AlumnoDTO oalumno = (new WSLogin("", "", "")).login();

			List<CursoDTO> ocursos =
							(new WSCursos("", oalumno.getCod_Alumno())).obtenerCurso();

		

			for (CursoDTO icurso : ocursos) {
				curso.create(icurso);
			}
			
				alumno.create(oalumno); 

		} catch (Exception e) {
		}
	}
}
